package com.weilus;

import com.weilus.refresh.ServiceConfigMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * Created by liutq on 2018/12/12.
 */
@RestController
@Slf4j
public class ProducerController {

    @Autowired
    ServiceConfigMap config;

    @PostMapping("/api/sayHello")
    public Mono<String> sayHello(@RequestBody Map<String, String> map){
        log.info("sayHello收到参数: {}",map);
        return Mono.just("flux-service-sayHello: 你好 "+config.getName());
    }

    @PostMapping("/api/hiMan")
    public Mono<String> hiMan(@RequestBody Map<String, String> map){
        log.info("hiMan收到参数:111222333 {}",map);
        if(map.containsKey("ex")){
            throw new RuntimeException("hiMan假装这儿发生了业务异常...");
        }
        return Mono.just("flux-service-hiMan: Hi "+config.getName());
    }

    @PostMapping("/api/loginLog")
    public Mono<String> loginLog(@RequestBody MQMessage msg) throws InterruptedException {
        log.info("loginLog收到参数: {}",msg);
        if(msg.getDelay()!=null){
            Thread.sleep(msg.getDelay());
        }
        return Mono.just("flux-service-loginLog: "+msg);
    }

    @PostMapping("/api/everyDayMoney")
    public Mono<String> everyDayMoney(@RequestBody MQMessage msg) throws InterruptedException {
        log.info("everyDayMoney收到参数: {}",msg);
        if(msg.getDelay()!=null){
            Thread.sleep(msg.getDelay());
        }
        if(msg.getEx()!=null && msg.getEx()){
            throw new RuntimeException("everyDayMoney假装这儿发生了业务异常...");
        }
        return Mono.just("flux-service-everyDayMoney: "+msg);
    }

}
